﻿
using Manon.Autofac;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

namespace Manon.ConfigCenter.Web.Common
{

    public class IdentityUser : IIdentityUser 
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        public IdentityUser(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }
        //public IdentityUser()
        //{
        //}

        //public IdentityUser GetIdentityUser()
        //{
        //    var auth = _httpContextAccessor.HttpContext.AuthenticateAsync();
        //    var _userId = auth.Result.Principal.Claims.First(t => t.Type.Equals("UserId")).Value;
        //    var userName = auth.Result.Principal.Claims.First(t => t.Type.Equals(ClaimTypes.Name)).Value;
        //    var RealName = auth.Result.Principal.Claims.First(t => t.Type.Equals("RealName")).Value;
        //    int _intUserId = 0;
        //    int.TryParse(_userId, out _intUserId);

        //    return new IdentityUser()
        //    {
        //        RealName = RealName,
        //        UserId = _intUserId,
        //        UserName = userName 
        //    };
        //}


        public int UserId
        {
            get
            {
                var auth = _httpContextAccessor.HttpContext.AuthenticateAsync();
                var _userId = auth.Result.Principal.Claims.First(t => t.Type.Equals("UserId")).Value;
                int _intUserId = 0;
                int.TryParse(_userId, out _intUserId);
                return _intUserId;
            }
            set { }
        }
        public string UserName
        {
            get
            {
                var auth = _httpContextAccessor.HttpContext.AuthenticateAsync();
                var userName = auth.Result.Principal.Claims.First(t => t.Type.Equals(ClaimTypes.Name)).Value;
                return userName;
            }

            set { }
        }
        public string RealName
        {
            get
            {
                var auth = _httpContextAccessor.HttpContext.AuthenticateAsync();
                var RealName = auth.Result.Principal.Claims.First(t => t.Type.Equals("RealName")).Value;
                return RealName;

            }

            set { }
        }
    }

}
